const router = require('koa-router')() // 一边导入一边实例化
// const result = require('../../config/result.js')
// const { regcheck, checking } = require('../../config/checking.js')
// 操作数据库的接口
const { getToken, Deleteurl, DeleteFileUrl, Updateurl } = require('../../config/databaseapi.js')
// 注册接口
router.post('/deletefileorfolder', async (ctx) => {
	console.log("调用删除文件/文件夹接口");
	console.log(ctx.request.body);
  let { currentFileOrFolder, currentListId, arr } = ctx.request.body;
  // console.log(arr);
  if(currentFileOrFolder.type === 'folder'){	
	  let query = `db.collection("study_materials").doc("${currentFileOrFolder._id}").remove()`;
	  let res = await new getToken().posteve(Deleteurl, query);
	  // console.log('删除结果', res)
	  if (res.errcode === 0) {
	    ctx.body = { msg: "删除文件夹成功", status: 1 }
	  } else {
	    ctx.body = { msg: "删除文件夹失败", status: 0 }
	  }
  }else{
	  let fileid_list = [];
	  fileid_list.push(currentFileOrFolder.file_id);
	  let res = await new getToken().posteve3(DeleteFileUrl,fileid_list);
	  // console.log(res);
	  if(res.errcode === 0){
		  let query = `db.collection("study_materials").doc("${currentFileOrFolder._id}").remove()`;
		  let res1 = await new getToken().posteve(Deleteurl, query);
		  if (res1.errcode === 0) {
		    ctx.body = { msg: "云存储删除成功,数据库删除成功", status: 1 }
		  } else {
		    ctx.body = { msg: "云存储删除成功,数据库删除失败", status: 0 }
		  }
	  }else{
		  ctx.body = { msg: "云存储删除失败", status: 0 }
	  }
  }
})

module.exports = router.routes()